﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using JiaBao.Services.LogService;

namespace JiaBao.WPF.Controls
{
    /// <summary>
    /// uc_Customer.xaml 的交互逻辑
    /// </summary>
    public partial class uc_Customer : UserControl
    {

        List<string> NoFileds = new List<string>() { "Id" };
        List<string> StaticFileds = new List<string>() { "姓名", "证件号码", "证件地址", "手机", "通讯地址" };
        List<string> LockedFileds = new List<string>() { "客户标识" };


        /// <summary>
        /// 是否初始化
        /// </summary>
        private bool isOk = false;

        private bool isShow = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public delegate void ThisClick(Dictionary<string, string> data);

        /// <summary>
        /// 关闭事件
        /// </summary>
        public event ThisClick Close;

        public event ThisClick Ok;

        #region Cort

        public uc_Customer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化控件数据
        /// </summary>
        public void Ini(IList<string> fileds)
        {
            try
            {
                if (isOk) return;

                isShow = false;
                //去除不显示
                fileds = fileds.Where(f =>
                {
                    if (!NoFileds.Any(n => f == n))
                        return true;
                    return false;
                }).ToList();

                //去除固定
                fileds = fileds.Where(f =>
                {
                    if (!StaticFileds.Any(n => f == n))
                        return true;
                    return false;
                }).ToList();


                //去除锁定
                fileds = fileds.Where(f =>
                {
                    if (!LockedFileds.Any(n => f == n))
                        return true;
                    return false;
                }).ToList();


                foreach (TextBox t in g_Static.Children)
                {
                    t.Text = "";

                    t.TextChanged += (object sender, TextChangedEventArgs e) =>
                    {
                        if (!isShow)
                        {
                            t.Tag = 1;
                        }
                    };
                    t.Tag = 0;
                }

                wp_Main.Children.Clear();
                for (int i = 0; i < fileds.Count; i++)
                {


                    var wp = new Grid() { Name = "wp_" + fileds[i], Margin = new Thickness(10), Width = 200 };

                    var l = new Label() { Content = fileds[i] };

                    var t = new TextBox() { Name = "txt_" + fileds[i], Width = 100, Tag = 0, ToolTip = fileds[i], HorizontalAlignment = HorizontalAlignment.Right };
                    t.TextChanged += (object sender, TextChangedEventArgs e) =>
                    {
                        if (!isShow)
                        {
                            t.Tag = 1;
                        }
                    };

                    wp.Children.Add(l);
                    wp.Children.Add(t);
                    wp_Main.Children.Add(wp);
                    wp_Main.RegisterName(Name = "txt_" + fileds[i], t);
                }

                isOk = true;
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// 调用显示
        /// </summary>
        /// <param name="data"></param>
        public void Show(Dictionary<string, string> data)
        {
            try
            {
                isShow = true;

                ClearValues();

                foreach (TextBox t in g_Static.Children)
                {
                    t.Text = data[t.ToolTip.ToString()];
                    t.Tag = 0;
                    data.Remove(t.ToolTip.ToString());
                }

                foreach (var i in LockedFileds) 
                    data.Remove(i);
              


                foreach (var item in data)
                {
                    var t = wp_Main.FindName("txt_" + item.Key) as TextBox;
                    t.Text = item.Value;
                    t.Tag = 0;
                }
                isShow = false;
                this.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearValues()
        {
            try
            {
                isShow = false;

                //固定
                foreach (TextBox t in g_Static.Children)
                {
                    t.Text = "";
                    t.Tag = 0;
                }
                //自定义
                foreach (Grid t in wp_Main.Children)
                {
                    ((TextBox)t.Children[1]).Text = "";
                    ((TextBox)t.Children[1]).Tag = 0;
                }

                isShow = true;
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, RoutedEventArgs e)
        {
            Close?.Invoke(null);
            Visibility = Visibility.Hidden;
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Ok_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Dictionary<string, string> r = new Dictionary<string, string>();

                //固定 
                foreach (TextBox t in g_Static.Children)
                {
                    if (1 == (int)t.Tag)
                    {
                        var k = t.ToolTip.ToString();
                        var v = t.Text.Trim();
                        r.Add(k, v);
                    }
                }

                //自定义
                foreach (Grid w in wp_Main.Children)
                {
                    if (1 == (int)((TextBox)w.Children[1]).Tag)
                    {
                        var k = ((Label)w.Children[0]).Content.ToString();
                        var v = ((TextBox)w.Children[1]).Text;
                        r.Add(k, v);
                    }
                }

                if (r.Count < 1) r = null;
                Ok?.Invoke(r);

            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
            Visibility = Visibility.Hidden;
        }
    }
}
